﻿namespace UnityEditor
{
    using System;
    using UnityEngine;

    internal class GameViewGUI
    {
        private static float m_ClientFrameTime;
        private static float m_ClientTimeAccumulator;
        private static int m_FrameCounter;
        private static float m_MaxFrameTime;
        private static float m_MaxTimeAccumulator;
        private static float m_RenderFrameTime;
        private static float m_RenderTimeAccumulator;
        private static GUIStyle s_SectionHeaderStyle;

        private static string FormatNumber(int num)
        {
            if (num < 0x3e8)
            {
                return num.ToString();
            }
            if (num < 0xf4240)
            {
                double num2 = num * 0.001;
                return (num2.ToString("f1") + "k");
            }
            double num3 = num * 1E-06;
            return (num3.ToString("f1") + "M");
        }

        public static void GameViewStatsGUI()
        {
            GUI.skin = EditorGUIUtility.GetBuiltinSkin(EditorSkin.Scene);
            GUI.color = new Color(1f, 1f, 1f, 0.75f);
            float width = 300f;
            float height = 200f;
            int length = Network.connections.Length;
            if (length != 0)
            {
                height += 220f;
            }
            GUILayout.BeginArea(new Rect((Screen.width - width) - 10f, 27f, width, height), "Statistics", GUI.skin.window);
            GUILayout.Label("Graphics:", sectionHeaderStyle, new GUILayoutOption[0]);
            UpdateFrameTime();
            string text = string.Format("{0:F1} FPS ({1:F1}ms)", 1f / Mathf.Max(m_MaxFrameTime, 1E-05f), m_MaxFrameTime * 1000f);
            GUI.Label(new Rect(170f, 19f, 120f, 20f), text);
            int usedTextureMemorySize = UnityStats.usedTextureMemorySize;
            int renderTextureBytes = UnityStats.renderTextureBytes;
            int screenBytes = UnityStats.screenBytes;
            int vboTotalBytes = UnityStats.vboTotalBytes;
            int bytes = screenBytes + renderTextureBytes;
            int num9 = (screenBytes + Mathf.Max(usedTextureMemorySize, renderTextureBytes)) + vboTotalBytes;
            GUILayout.Label(string.Format("  Main Thread: {0:F1}ms  Renderer: {1:F1}ms\n", m_ClientFrameTime * 1000f, m_RenderFrameTime * 1000f) + string.Format("  Draw Calls: {0} \tSaved by batching: {1} \n", UnityStats.drawCalls, UnityStats.batchedDrawCalls - UnityStats.batches) + string.Format("  Tris: {0} \tVerts: {1} \n", FormatNumber(UnityStats.triangles), FormatNumber(UnityStats.vertices)) + string.Format("  Used Textures: {0} - {1}\n", UnityStats.usedTextureCount, EditorUtility.FormatBytes(usedTextureMemorySize)) + string.Format("  Render Textures: {0} - {1} \tswitches: {2}\n", UnityStats.renderTextureCount, EditorUtility.FormatBytes(renderTextureBytes), UnityStats.renderTextureChanges) + string.Format("  Screen: {0} - {1}\n", UnityStats.screenRes, EditorUtility.FormatBytes(screenBytes)) + string.Format("  VRAM usage: {0} to {1} (of {2})\n", EditorUtility.FormatBytes(bytes), EditorUtility.FormatBytes(num9), EditorUtility.FormatBytes((SystemInfo.graphicsMemorySize * 0x400) * 0x400)) + string.Format("  VBO Total: {0} - {1}\n", UnityStats.vboTotal, EditorUtility.FormatBytes(vboTotalBytes)) + string.Format("  Shadow Casters: {0} \n", UnityStats.shadowCasters) + string.Format("  Visible Skinned Meshes: {0} \t Animations: {1}", UnityStats.visibleSkinnedMeshes, UnityStats.visibleAnimations), new GUILayoutOption[0]);
            if (length != 0)
            {
                GUILayout.Label("Network:", sectionHeaderStyle, new GUILayoutOption[0]);
                GUILayout.BeginHorizontal(new GUILayoutOption[0]);
                for (int i = 0; i < length; i++)
                {
                    GUILayout.Label(UnityStats.GetNetworkStats(i), new GUILayoutOption[0]);
                }
                GUILayout.EndHorizontal();
            }
            else
            {
                GUILayout.Label("Network: (no players connected)", sectionHeaderStyle, new GUILayoutOption[0]);
            }
            GUILayout.EndArea();
        }

        private static void UpdateFrameTime()
        {
            if (Event.current.type == EventType.Repaint)
            {
                float frameTime = UnityStats.frameTime;
                float renderTime = UnityStats.renderTime;
                m_ClientTimeAccumulator += frameTime;
                m_RenderTimeAccumulator += renderTime;
                m_MaxTimeAccumulator += Mathf.Max(frameTime, renderTime);
                m_FrameCounter++;
                bool flag = (m_ClientFrameTime == 0f) && (m_RenderFrameTime == 0f);
                bool flag2 = ((m_FrameCounter > 30) || (m_ClientTimeAccumulator > 0.3f)) || (m_RenderTimeAccumulator > 0.3f);
                if (flag || flag2)
                {
                    m_ClientFrameTime = m_ClientTimeAccumulator / ((float) m_FrameCounter);
                    m_RenderFrameTime = m_RenderTimeAccumulator / ((float) m_FrameCounter);
                    m_MaxFrameTime = m_MaxTimeAccumulator / ((float) m_FrameCounter);
                }
                if (flag2)
                {
                    m_ClientTimeAccumulator = 0f;
                    m_RenderTimeAccumulator = 0f;
                    m_MaxTimeAccumulator = 0f;
                    m_FrameCounter = 0;
                }
            }
        }

        private static GUIStyle sectionHeaderStyle
        {
            get
            {
                if (s_SectionHeaderStyle == null)
                {
                    s_SectionHeaderStyle = EditorGUIUtility.GetBuiltinSkin(EditorSkin.Scene).GetStyle("BoldLabel");
                }
                return s_SectionHeaderStyle;
            }
        }
    }
}

